package com.zjh.photoshopstudy.Utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.Model.HistoryEntity;
import com.zjh.photoshopstudy.Model.VideoEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static android.content.Context.MODE_PRIVATE;

public class SharePreferencesUtils {


    public static void saveLoginUser(AppUserEntity user, Context context) {
        Gson gson = new Gson();
        String json = gson.toJson(user);
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("loginUser", json);
        edit.apply();
    }

    public static void clearLoginUser(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("loginUser", null);
        edit.apply();
    }

    public static AppUserEntity getLoginUser(Context context) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        String json = sp.getString("loginUser", null);
        if (json != null && !json.isEmpty()) {
            return gson.fromJson(json, AppUserEntity.class);
        }
        return null;
    }

    public static void saveHistory(HistoryEntity historyEntity, Context context) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("history", MODE_PRIVATE);
        String historyJson = sp.getString("history", "");
        List<HistoryEntity> list = gson.fromJson(historyJson, new TypeToken<List<HistoryEntity>>() {
        }.getType());
        if (list == null) {
            list = new ArrayList<>();
        }
        for (HistoryEntity e : list) {
            if (historyEntity.getId() == e.getId()) {
                list.remove(e);
                break;
            }
        }
        list.add(historyEntity);
        historyJson = gson.toJson(list);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("history", historyJson);
        edit.apply();
    }

    public static void deleteHistory(List<HistoryEntity> historyList, Context context) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("history", MODE_PRIVATE);
        String historyJson = sp.getString("history", "");
        List<HistoryEntity> list = gson.fromJson(historyJson, new TypeToken<List<HistoryEntity>>() {
        }.getType());
        for (int i = 0; i < list.size(); i++) {
            HistoryEntity e = list.get(i);
            for (HistoryEntity e1 : historyList) {
                if (e.getId() == e1.getId()) {
                    list.remove(e);
                    i--;
                    break;
                }
            }
        }
        historyJson = gson.toJson(list);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("history", historyJson);
        edit.apply();
    }

    public static List<HistoryEntity> getHistory(Context context) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("history", MODE_PRIVATE);
        String historyJson = sp.getString("history", "");
        if (historyJson != null) {
            return gson.fromJson(historyJson, new TypeToken<List<HistoryEntity>>() {
            }.getType());
        }
        return null;
    }

    public static void saveCollection(Context context, int uid, int vid) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("collection", MODE_PRIVATE);
        String historyJson = sp.getString("collection", "");
        Map<Integer, Set<Integer>> map = gson.fromJson(historyJson, new TypeToken<Map<Integer, Set<Integer>>>() {
        }.getType());
        if (map == null) {
            map = new HashMap<Integer, Set<Integer>>();
        }
        Set<Integer> set = map.get(uid);
        if (set == null) {
            set = new HashSet<>();
        }
        set.add(vid);
        map.put(uid, set);
        historyJson = gson.toJson(map);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("collection", historyJson);
        edit.apply();
    }

    public static void deleteCollection(Context context, int uid, Set<Integer> vid) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("collection", MODE_PRIVATE);
        String historyJson = sp.getString("collection", "");
        Map<Integer, Set<Integer>> map = gson.fromJson(historyJson, new TypeToken<Map<Integer, Set<Integer>>>() {
        }.getType());
        if (map != null) {
            Set<Integer> set = map.get(uid);
            set.removeAll(vid);
            map.put(uid, set);
        }
        historyJson = gson.toJson(map);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("collection", historyJson);
        edit.apply();
    }

    public static void clearCollection(Context context, int uid) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("collection", MODE_PRIVATE);
        String historyJson = sp.getString("collection", "");
        Map<Integer, Set<Integer>> map = gson.fromJson(historyJson, new TypeToken<Map<Integer, Set<Integer>>>() {
        }.getType());
        if (map != null) {
            Set<Integer> set = map.get(uid);
            set.clear();
            map.put(uid, set);
        }
        historyJson = gson.toJson(map);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("collection", historyJson);
        edit.apply();
    }

    public static Set<Integer> getCollection(Context context, int uid) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("collection", MODE_PRIVATE);
        String historyJson = sp.getString("collection", "");
        Map<Integer, Set<Integer>> map = gson.fromJson(historyJson, new TypeToken<Map<Integer, Set<Integer>>>() {
        }.getType());
        if (map != null) {
            return map.get(uid);
        }
        return null;
    }


    public static void saveNewCommentCount(Context context, int uid, int size) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("comment", MODE_PRIVATE);
        String commentJson = sp.getString("comment", "");
        Map<Integer, Integer> map = gson.fromJson(commentJson, new TypeToken<Map<Integer, Integer>>() {
        }.getType());
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(uid, size);
        commentJson = gson.toJson(map);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("comment", commentJson);
        edit.apply();
    }

    public static int getNewCommentCount(Context context, int uid) {
        Gson gson = new Gson();
        SharedPreferences sp = context.getSharedPreferences("comment", MODE_PRIVATE);
        String commentJson = sp.getString("comment", "");
        Map<Integer, Integer> map = gson.fromJson(commentJson, new TypeToken<Map<Integer, Integer>>() {
        }.getType());
        if (map != null) {
            return map.get(uid);
        }
        return 0;
    }
}
